<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
  <head>
    <base href="<%=basePath%>">
	<%@ include file="/base/jsp/include.jsp"%>
  </head>
  <body>
    <form id="form" method="post">
        <input name="id" class="mini-hidden" />
        <fieldset style="border:solid 1px #aaa;padding:10px;">
            <legend >添加笔记作者</legend>
            <div style="padding:5px;">
            <table class="editor_table">
            	<tr>
                    <td style="width:200px;">名称：</td>
                    <td style="width:150px;">
                    	<input name="name" id="name" class="mini-textbox editor_len" required="true" vtype="maxLength:150" emptyText="请输入姓名"/>
                    </td>
                </tr>
               
                <tr>
                    <td style="width:200px;">描述：</td>
                    <td style="width:150px;">
                    	<textarea name="description" id="description" class="mini-textarea editor_len" required="true" emptyText=""></textarea>
                    </td>
                </tr>
                
                <tr>
                    <td style="width:200px;">口号：</td>
                    <td style="width:150px;">
                    	<textarea name="slogan" id="slogan" class="mini-textarea editor_len" required="true" emptyText=""></textarea>
                    </td>
                </tr>
                
                <tr>
                    <td style="width:200px;">关键字：</td>
                    <td style="width:150px;">
                    	<textarea name="areas" id="areas" class="mini-textarea editor_len" required="true" emptyText="不同字间用英文冒号分隔，例如：赢;应"></textarea>
                    </td>
                </tr>
                
                <tr>
                    <td style="width:200px;">选择类型：</td>
                    <td style="width:150px;">
                    	 <input name="type" id="type" value="" required="true" class="mini-combobox" textField="text" valueField="id" emptyText="类型"
                        url="${path}/base/json/essenceAuthorType.json" allowInput="false" onvaluechanged="onFilterChanged" showNullItem="true" nullItemText="请选择..."/>
                    </td>
                </tr>
                
                <tr>
                    <td style="width:200px;">选择科目：</td>
                    <td style="width:150px;">
                    	 <input name="subject" id="subject" value="" required="true" class="mini-combobox" textField="text" valueField="id" emptyText="科目"
                        url="${path}/base/subjectForNote.json" allowInput="false" onvaluechanged="onFilterChanged" showNullItem="true" nullItemText="请选择..."/>
                    </td>
                </tr>

              	<tr>
                    <td style="width:70px;">所属机构：</td>
                    <td style="width:150px;">
                    <div id="belong" class="mini-listbox"  url="getEssenceAuthors.do" valueField="id" showCheckBox="true" multiSelect="true">     
                        <div property="columns">
                            <div header="机构" field="name"></div>
                         </div>
                     </div>

                    </td>
               </tr>
               
               <tr style="display:none;" id="courseslist-tr">
                    <td style="width:70px;">添加班级：</td>
                    <td style="width:150px;">
                    <div id="courseslist" class="mini-listbox"  url="getEssenceCourses.do" valueField="name" showCheckBox="true" multiSelect="true">     
                        <div property="columns">
                            <div header="课程" field="name"></div>
                         </div>
                     </div>

                    </td>
               </tr>
               
               <tr style="display:none;">
                    <td style="width:200px;">所属机构id：</td>
                    <td style="width:150px;">
                    	<input name="belongid" id="belongid" class="mini-textbox" required="true" emptyText=""/>
                    </td>
                </tr>
                
                <tr style="display:none;">
                    <td style="width:200px;">所在班级</td>
                    <td style="width:150px;">
                    	<input name="courses" id="courses" class="mini-textbox" required="true" emptyText=""/>
                    </td>
                </tr>
                
                <tr>
                    <td style="width:70px;">上传头像：</td>
                    <td style="width:150px;">
                    	<!--fileCountLimit:上限 multi:是否为多文件 showImage:是否为图片  fileType:文件类型 fileSize:文件大小限制 buttonText:按钮文本 -->
                      <input id="file_upload" fileCountLimit="1" multi="true" type="file" 
                      	showImage="true" onuploadsuccess="onUploadSuccess"  fileType="*.jpg;*.jpeg;*.png" fileSize="1MB" buttonText="上传图标"/>
                    </td>
                </tr>
                 <tr style="display:none;">
                    <td style="width:200px;">文件ID：</td>
                    <td style="width:150px;">
                    	<input name="headimg" id="headimg" class="mini-textbox" required="true" vtype="maxLength:100" emptyText="图片id"/>
                    </td>
                </tr>
               
                
            </table>            
         </div>
        </fieldset>
        
        <div style="text-align:center;padding:10px;">
            <a class="mini-button" onclick="onOk" style="width:60px;margin-right:20px;">确定</a>       
            <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>    
        </div>        
    </form>
    
    <script type="text/javascript">
    	var id = '';
        mini.parse();
        var form = new mini.Form("form");
        function SaveData() {
			var o = form.getData();
			 form.validate();
            if (form.isValid() == false) return;
            var json = mini.encode(o);
            $.ajax({
                url: "saveEssenceAuthor.do",
                type: 'post',
                data: $.evalJSON(json),
                cache: false,
                success: function (text) {
                	if(text.indexOf('true') > -1){
				    	mini.alert("操作成功");
				    	CloseWindow("save");
				    }else{
				    	mini.alert("操作失败");
				    	CloseWindow();
				    }
                },
                error: function (textStatus, errorThrown) {
                    alert(jqXHR.responseText);
                    CloseWindow();
                }
            });
        }
       
        
        //标准方法接口定义
        function SetData(data) {
            if (data.action == "edit") {
                //跨页面传递的数据对象，克隆后才可以安全使用
                data = mini.clone(data);
                $("#courseslist-tr").show();
                $.ajax({
                    url: "findByIdEssenceAuthor.do?id=" + data.id,
                    cache: false,
                    success: function (text) {
                        var o = mini.decode(text);
                        form.setData(o);
                        form.setChanged(false);
                        id = o.id;
                        var imgs = mini.get("headimg").getValue();
                        if(imgs != ''){
               			 $('#file_upload').setFileId(imgs,true,true);
               		    }
               		    var belongs = o.belongid;
               		    var courses = o.courses;
               		    var listbox = mini.get("belong");
               		    var courselistbox = mini.get("courseslist");
               		    for(var i = 0 ; i < listbox.getCount();i++){
               		    	var item = listbox.getAt(i);
               		    	if(belongs.indexOf(item.id) > -1){
               		    		listbox.select(item);
               		    	}
               		    }
               		    courselistbox.load("getEssenceCourses.do?type="+o.subject);
               		    for(var i=0;i<courselistbox.getCount();i++){
               		    var item = courselistbox.getAt(i);
               		    	if(courses.indexOf(item.name) > -1){
               		    		courselistbox.select(item);
               		    	}
               		    }
               		    
                    }
                });
            }
        }

        function GetData() {
            var o = form.getData();
            return o;
        }
        
        function CloseWindow(action) {
            if (action == "cancel" && form.isChanged()) {
                if (confirm("数据被修改了，是否先保存？")) {
                	SaveData();
                	return false;
                }
            }
            if (window.CloseOwnerWindow) return window.CloseOwnerWindow(action);
            else window.close();            
        }
        
        function onOk(e) {
        	var imgId = mini.get("headimg").getValue();
        	if(imgId == ""){
        		mini.alert("请上传作者头像");
        		return ;
        	}
        	mini.get("belongid").setValue(mini.get("belong").getValue());
        	mini.get("courses").setValue(mini.get("courseslist").getValue());
            SaveData();
        }
        function onCancel(e) {
            CloseWindow("cancel");
        }
       
        function onUploadSuccess(e) {
             mini.get("headimg").setValue($("#file_upload").getFileId());
        }
    </script>
  </body>
</html>
